package org.karelun.rabbit.rcp.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.net4j.CDONet4jSession;
import org.eclipse.emf.cdo.net4j.CDONet4jSessionConfiguration;
import org.eclipse.emf.cdo.net4j.CDONet4jUtil;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.view.CDOAdapterPolicy;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.net4j.Net4jUtil;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.util.container.IPluginContainer;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.karelun.rabbit.graph.Diagram;
import org.karelun.rabbit.graph.GraphFactory;
import org.karelun.rabbit.graph.GraphPackage;
import org.karelun.rabbit.rcp.editor.RabbitEditorInput;

public class OpenEditorHandler extends OpenViewHandler {
	
	@Override
	protected CDOView openView(CDONet4jSession session) {
		return session.openTransaction();
	}
	
	@Override
	protected void openEditor(IWorkbenchPage page, CDOView cdoView) {
		CDOTransaction transaction=(CDOTransaction)cdoView;
		String resourcePath = getResourcePathName();
		if(!transaction.hasResource(resourcePath)){
			CDOResource resource = transaction.createResource(resourcePath);
			Diagram diagram = GraphFactory.eINSTANCE.createDiagram();
			resource.getContents().add(diagram);
			try {
				transaction.commit();
			} catch (CommitException e) {
				e.printStackTrace();
			}
		}
		try {
			//Fill the resource into transaction's resource set.
			String path = transaction.getResource(resourcePath).getPath();
			RabbitEditorInput editorInput = new RabbitEditorInput(cdoView, path);
			page.openEditor(editorInput, "org.karelun.emf.rabbit.editor");
		 } catch (PartInitException e) {
			 e.printStackTrace();
		 }
	}
	
	public Object execute1(ExecutionEvent event) throws ExecutionException {
		IWorkbenchPage page =PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();

		String server = "localhost";
		String respository = "rabbit";

		IConnector connector = Net4jUtil.getConnector(IPluginContainer.INSTANCE, "tcp", server);
		CDONet4jSessionConfiguration config = CDONet4jUtil.createNet4jSessionConfiguration();
		config.setConnector(connector);
		config.setRepositoryName(respository);

		CDONet4jSession session = config.openNet4jSession();
		session.getPackageRegistry().putEPackage(GraphPackage.eINSTANCE);
		CDOTransaction transaction = session.openTransaction();
		transaction.options().addChangeSubscriptionPolicy(CDOAdapterPolicy.ALL);

//		CDOResource resource = transaction.createResource("rsdj");
//		Diagram diagram = GraphFactory.eINSTANCE.createDiagram();
//		diagram.setText("hhh");
//		resource.getContents().add(diagram);
//		try {
//			transaction.commit();
//		} catch (CommitException e) {
//			e.printStackTrace();
//		}
		String resourcePath = transaction.getResource("/rsdj").getPath();
		try {
			 RabbitEditorInput editorInput = new RabbitEditorInput(transaction, resourcePath);
			 page.openEditor(editorInput, "org.karelun.emf.rabbit.editor");
		 } catch (PartInitException e) {
		 e.printStackTrace();
		 }
		return null;
	}

	private IFile createDigramFile() {
		IFile file = ResourcesPlugin.getWorkspace().getRoot()
				.getFile(new Path("diagram.xml"));
		ResourceSetImpl resourceSet = new ResourceSetImpl();
		URI fileURI = URI.createPlatformPluginURI(
				file.getFullPath().toString(), true);
		Resource resource = resourceSet.createResource(fileURI);
		Diagram diagram = GraphFactory.eINSTANCE.createDiagram();
		resource.getContents().add(diagram);
		Map<Object, Object> options = new HashMap<Object, Object>();
		options.put(XMLResource.OPTION_ENCODING, "UTF-8");
		try {
			resource.save(options);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file;
	}

}
